package cn.schoolwow.util.test.module.replaceFile.ruoyi.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.resource.QueryResource;
import cn.schoolwow.util.domain.query.resource.QueryResourceResponse;
import cn.schoolwow.util.domain.replace.ReplaceFile;
import cn.schoolwow.util.module.query.resource.domain.SingleQueryResourceHandler;

import java.io.File;
import java.nio.file.Path;

public class ModifyPomFileFlow implements BusinessFlow {
    private static String[] prefixPackageNameArray = new String[]{
            "org.dromara","com.ruoyi"
    };

    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String directoryPath = flowContext.checkData("directoryPath", String.class);
        String projectName = flowContext.checkData("projectName", String.class);
        String groupIdName = flowContext.checkData("groupIdName",String.class);
        Path basePath = flowContext.checkData("basePath", Path.class);

        QueryResource.newQuery("pom.xml文件", directoryPath)
                .fileNameEndWith(".xml",".yml")
                .singleQueryResourceHandler(new SingleQueryResourceHandler() {
                    @Override
                    public boolean handleQueryResourceResponse(QueryResourceResponse queryResourceResponse) {
                        File file = queryResourceResponse.getAsFile();
                        ReplaceFile replaceFile = ReplaceFile.queryFile(file)
                                .addReplaceString("ruoyi-", projectName+"-")
                                .addReplaceString("<artifactId>ruoyi</artifactId>", "<artifactId>"+projectName+"</artifactId>");
                        for(String prefixPackageName:prefixPackageNameArray){
                            replaceFile.addReplaceString("<groupId>"+prefixPackageName+"</groupId>", "<groupId>"+groupIdName+"</groupId>")
                                    .addReplaceString(prefixPackageName, groupIdName);
                        }
                        replaceFile.replaceFile();
                        String sourcePath = basePath.relativize(file.toPath()).toString();
                        System.out.println("替换pom文件["+sourcePath+"]");
                        return true;
                    }
                })
                .execute()
                .walkResource();
    }

    @Override
    public String name() {
        return "修改pom.xml文件";
    }
}
